﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Common
{
    public  class OperationText
    {
        public void WriteTXT(string filepath, string fileName, string writetxt)
        {

            try
            {
                string path1 = filepath + "\\" + fileName;
 
            if (path1 != "")
            {
                if (File.Exists(path1))
                {
                    File.Delete(path1);
                }
                else
                {
                    GetDirPath(filepath);
                }

                FileStream fs11 = new FileStream(path1, FileMode.Create, FileAccess.Write);//创建写入文件 

                System.Text.Encoding.GetEncoding("gb2312");
                StreamWriter sw1 = new StreamWriter(fs11, System.Text.Encoding.Default);
 
                sw1.WriteLine(writetxt);
                sw1.Close();
            }
            }
            catch (Exception ex)
            {

                Mdl_Class.WriteLog.sysErrLog("WriteTXT()", ex.ToString());
            }
        }


        public  string ReadTXT(string filepath)
        {
            System.Text.Encoding.GetEncoding("gb2312");
        
            string readtxt = "";
            if (File.Exists(filepath))
            {
                string[] lines = System.IO.File.ReadAllLines(filepath, System.Text.Encoding.Default);
                if (lines.Length > 0)
                {
                    readtxt = lines[0];
                }
            }
            return readtxt;
        }


        private  string GetDirPath(string path)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(path);
            if (dirInfo.Exists)
            {
                return string.Format(@"{0}\", dirInfo.FullName);
            }
            else
            {
                try
                {
                    dirInfo.Create();
                }
                catch (IOException e)
                {
                    Console.WriteLine(e.Message);
                    return Environment.CurrentDirectory + @"\Log";
                }

                return string.Format(@"{0}\", dirInfo.FullName);
            }
        }


        public List<float> ReadOldData(string filepath) 
        {
            System.Text.Encoding.GetEncoding("gb2312");
            List<float> readdatas = new List<float> ();
           
            if (File.Exists(filepath))
            {
                string[] lines = System.IO.File.ReadAllLines(filepath, System.Text.Encoding.Default);
                if (lines.Length > 0)
                {
                    string[] readdatasStrings = lines[0].Split(',');
                    for (int i = 0; i < readdatasStrings.Length; i++)
                    {
                        double aaa = Convert.ToDouble(readdatasStrings[i]);
                        readdatas.Add(Convert.ToSingle(Math.Round(aaa,5)));
                    }
                }
            }
            return readdatas;
        }



    }
}

